Page({
  data: {
    cancelColor: "#888",
    color: "#EB0909",
    startYear: 2020,
    endYear: 2030,
    startDate: '',
    endDate: '',
    pageIndex: 1,
    loading: false,
    noMore: false,
    list: null
  },
  onLoad: function () {
    this.datetime = this.selectComponent("#rf-datetime-ctx");
    this.getFootprint();
  },
  onReachBottom: function () {
    if (this.data.noMore) return;
    this.setData({
      loading: true
    });
    this.getFootprint();
  },
  getFootprint: function () {
    let query = `page=${this.data.pageIndex}`,
        startDate = wx.time.toUnix(this.data.startDate),
        endDate = wx.time.toUnix(this.data.endDate);
    if (startDate) {
      query += `&start_time=${startDate}`;
    }
    if (endDate) {
      query += `&end_time=${endDate}`;
    }
    wx.util.request('user.footprint.list', {
      query: query
    }).then(footprint => {
      let list = this.data.list || {};
      if (footprint.length > 0) {
        for (let i in footprint) {
          // 返回的日期做为分组key
          let key = wx.time.toDate(footprint[i].updated_at, 'Y-M-D'),
              array = list[key] || [],
              [int, float] = footprint[i].product.minPriceSku.price.split('.');
          if (float == '00') {
            footprint[i].product.minPriceSku.price = int;
          }
          array.push(footprint[i]);
          list[key] = array;
        }
      }
      this.setData({
        list: list,
        loading: false,
        noMore: footprint.length < 10,
        pageIndex: this.data.pageIndex + 1
      });
    });
  },
  selectDate: function (e) {
    let type = e.currentTarget.dataset.type;
    this.setData({
      curDateType: type,
      curDateTime: this.data[type]
    });
    this.datetime.show();
  },
  cofirmDate: function (e) {
    this.setData({
      [this.data.curDateType]: e.detail.result
    });
  },
  retrieve: function () {
    this.getFootprint();
  },
  delete: function (e) {
    let index = e.currentTarget.dataset.index,
        idx = e.currentTarget.dataset.idx,
        list = this.data.list,
        item = list[index];
    wx.util.request('user.footprint.del', {
      query: `id=${item[idx].id}`
    }).then(() => {
      item.splice(idx, 1);
      if (!item.length) {
        delete list[index];
        this.setData({
          list: list
        });
      } else {
        this.setData({
          [`list.${index}`]: item
        });
      }
    });
  }
})